package com.dc.service.impl;

import com.dc.dao.OrderDao;
import com.dc.em.ErrorMessageEnum;
import com.dc.model.Order;
import com.dc.model.TSOrder;
import com.dc.model.USOrder;
import com.dc.model.UserOrder;
import com.dc.service.OrderService;
import com.dc.vo.BaseModel;
import com.dc.vo.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderDao orderDao;
    public BaseModel selectPageOrder(String user_name, Integer curr_page, Integer page_size, BaseModel baseModel) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<USOrder> orders = orderDao.selectPageOrder(user_name);
        PageInfo pageInfo = new PageInfo(orders,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询订单成功");
        baseModel.setData(pageInfo);
        return baseModel;
    }

    public BaseModel addOrder(Order order, BaseModel baseModel) throws Exception {
        int result = orderDao.insertOrder(order);
        if(result<=0){
            throw new BusinessException(ErrorMessageEnum.ADD_SHOES_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增成功");
        return baseModel;
    }

    public BaseModel deletedOrder(String ids, BaseModel baseModel) throws Exception {
        String[] idArry = ids.split(",");
        int result = orderDao.deletedOrder(idArry);
        if(result <=0 ){
            throw new BusinessException(ErrorMessageEnum.DELETED_ORDER_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result+"条记录成功");
        return baseModel;
    }

    public BaseModel updateOrder(Order order, BaseModel baseModel) throws Exception {
        int result = orderDao.updateOrder(order);
        if(result <= 0){
            throw new BusinessException(ErrorMessageEnum.UPDATE_ORDER_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改订单成功");
        return baseModel;
    }

    public BaseModel selectOrderById(int id, BaseModel baseModel,Integer curr_page,Integer page_size) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<UserOrder> orders = orderDao.selectOrderById(id);
        PageInfo pageInfo = new PageInfo(orders,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询订单成功");
        baseModel.setData(orders);
        return baseModel;
    }

    public BaseModel selectOrderByid(int id, BaseModel baseModel) throws Exception {
        Order orders = orderDao.selectOrderByid(id);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询订单成功");
        baseModel.setData(orders);
        return baseModel;
    }

    public BaseModel selectOrderInfoById(int id, BaseModel baseModel) throws Exception {
        TSOrder tsOrders = orderDao.selectOrderInfoById(id);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询详情成功");
        baseModel.setData(tsOrders);
        return baseModel;
    }
//    查询未通过订单
    public BaseModel selectPageNOPassOrder(int id,Integer curr_page, Integer page_size, BaseModel baseModel) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<Order> orders = orderDao.selectPageNOPassOrder(id);
        PageInfo pageInfo = new PageInfo(orders,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询成功");
        baseModel.setData(pageInfo);
        return baseModel;
    }

    public BaseModel selectPagePassOrder(int id,Integer curr_page, Integer page_size, BaseModel baseModel) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<Order> orders = orderDao.selectPagePassOrder(id);
        PageInfo pageInfo = new PageInfo(orders,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询成功");
        baseModel.setData(pageInfo);
        return baseModel;
    }

    public BaseModel updateOrderState(int id, int state, BaseModel baseModel) throws Exception {
        int result = orderDao.updateOrderState(id,state);
        if(result <=0){
            throw new BusinessException(ErrorMessageEnum.UPDATE_ORDER_ERROR_STATE);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("订单审核成功");
        return baseModel;
    }
}
